package contegor.scire.apps;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.util.ValidationEventCollector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import contegor.scire.core.IdFragment;
import contegor.scire.core.StorageResultsReturnPoint;
import contegor.scire.core.StringIdFragment;
import contegor.scire.persistence.xml.XMLConceptCloud;
import contegor.scire.structure.ConceptCloud;
import contegor.scire.structure.Connection;
import contegor.scire.structure.DockingModule;
import contegor.scire.structure.FilesContainer;
import contegor.scire.structure.FilesSynchronousConceptCloud;
import contegor.scire.structure.IntraProcessDockingModule;
import contegor.scire.structure.IntraprocessConnection;
import contegor.scire.structure.JpegsContainer;
import contegor.scire.structure.TestFilesContainer;

public class Desktop {
  private static final Logger logger = LoggerFactory.getLogger(TestFilesContainer.class);

  protected XMLConceptCloud createXmlConceptCloud(File file, IdFragment id) throws Exception {
    if (file.isFile())
      return createXmlConceptCloud(new FileInputStream(file));
    XMLConceptCloud cloud = new XMLConceptCloud(id);
    ValidationEventCollector veh = new ValidationEventCollector();
    cloud.save(new FileOutputStream(file), veh);
    if (veh.hasEvents())
      for (ValidationEvent event: veh.getEvents())
        logger.error(event.toString());
    return cloud;
  }

  protected XMLConceptCloud createXmlConceptCloud(InputStream is) throws Exception {
    StorageResultsReturnPoint<Void> crp = new StorageResultsReturnPoint<Void>();
    ValidationEventCollector veh = new ValidationEventCollector();
    XMLConceptCloud coreCloud = new XMLConceptCloud(is, veh, crp);
    if (veh.hasEvents())
      for (ValidationEvent event: veh.getEvents())
        logger.error(event.toString());
    if (crp.exceptions != null) {
      for (Exception ex: crp.exceptions)
        logger.error(ex.getMessage(), ex);
      throw crp.exceptions.get(0);
    }
    return coreCloud;
  }

  protected Connection createFullCloud(String filesPath, String jpegsPath, String langPath, String miscPath) throws Exception {
    File files = new File(filesPath);
    File jpegs = new File(jpegsPath);
    ConceptCloud coreCloud  = createXmlConceptCloud(ClassLoader.getSystemResourceAsStream("base.xml"));
    ConceptCloud filesCloud = new FilesSynchronousConceptCloud(new StringIdFragment("files"), new FilesContainer(files.getCanonicalPath(), files), new JpegsContainer(jpegs.getCanonicalPath(), jpegs));
    ConceptCloud langCloud  = createXmlConceptCloud(new File(langPath), new StringIdFragment("lang"));
    ConceptCloud miscCloud  = createXmlConceptCloud(new File(miscPath), new StringIdFragment("misc"));

    StorageResultsReturnPoint<Void> crp = new StorageResultsReturnPoint<Void>();
    DockingModule dmodule = new IntraProcessDockingModule(4, crp);
    coreCloud.dock(dmodule);
    filesCloud.dock(dmodule);
    langCloud.dock(dmodule);
    miscCloud.dock(dmodule);
    return new IntraprocessConnection(dmodule);
  }

  public static void main(String ... args) throws Exception {
    Desktop desktop = new Desktop();
    desktop.createFullCloud("/tmp/files","/tmp/jpegs","/tmp/lang.xml","/tmp/misc.xml");
  }
}